#define _LED_MODULE_

#include "ledctr.h"
#include "pincfg.h"
#include "systask.h"

#include "board.h"
#include "fsl_gpio.h"
#include "clock_config.h"
#include "pin_mux.h"

#include "fsl_ctimer.h"

#include "dataproc.h"
#include "osObjects.h" 

#define CTIMER CTIMER2                 /* Timer 2 */

uint32_t period;
led_breath_type led_brt __attribute__((section(".app_mem." "end")));
uint16_t duty_buff[1024] __attribute__((section(".app_mem." "1")));
uint16_t duty_index __attribute__((section(".app_mem." "2")));

void DisplayClr(void);
void DispUpdate(void);
void LedInit(void);

/* Define the init structure for the output LED pin*/
gpio_pin_config_t led_config = {
    kGPIO_DigitalOutput,
    0,
};

/*****************************************************************************//*!
*
* @brief   led task.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

void led_task(void   *argument) {
    
    /* Init output LED GPIO. */
    INTX_DISABLE();
    GPIO_PortInit(GPIO, BOARD_LED_BLUE_GPIO_PORT);
    GPIO_PinInit(GPIO, BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN, &led_config);
    GPIO_PinInit(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, &led_config);
    GPIO_PinInit(GPIO, BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN, &led_config);
    INTX_ENABLE();
    
    LedInit();
  
    while(1)
    {
        osDelay(3);
        
        led_brt.IncCoef = 64;
        led_brt.min = 10;
        led_brt.max = 20000;
        led_brt.max_keep_time_sv = 10;
        led_brt.min_keep_time_sv = 10;
        
        LedBreathHandle(&led_brt);
        
        duty_index++;
        duty_index &= 0x3FF;
        duty_buff[duty_index] = led_brt.Out;
        
        CTIMER->MR[1] = duty_buff[duty_index];
        CTIMER->MR[2] = duty_buff[(duty_index-1000)&0x3FF];
        
        //GPIO_PortSet(GPIO, BOARD_LED_BLUE_GPIO_PORT, 1u << BOARD_LED_BLUE_GPIO_PIN);
        //GPIO_PortClear(GPIO, BOARD_LED_BLUE_GPIO_PORT, 1u << BOARD_LED_RED_GPIO_PIN);
        //GPIO_PortSet(GPIO, BOARD_LED_BLUE_GPIO_PORT, 1u << BOARD_LED_GREEN_GPIO_PIN);
    }
}

/*******************************************************************************
 * Code
 ******************************************************************************/

void LedInit(void)
{
    ctimer_config_t config;
    uint32_t srcClock_Hz;

    INTX_DISABLE();
    const uint32_t port1_pin4_config = (/* Pin is configured as CTIMER2_MAT1 */
                                        IOCON_PIO_FUNC3 |
                                        /* No addition pin function */
                                        IOCON_PIO_MODE_INACT |
                                        /* Standard mode, output slew rate control is enabled */
                                        IOCON_PIO_SLEW_STANDARD |
                                        /* Input function is not inverted */
                                        IOCON_PIO_INV_DI |
                                        /* Enables digital function */
                                        IOCON_PIO_DIGITAL_EN |
                                        /* Open drain is disabled */
                                        IOCON_PIO_OPENDRAIN_DI);
    /* PORT1 PIN4 (coords: 1) is configured as CTIMER2_MAT1 */
    IOCON_PinMuxSet(IOCON, 1U, 4U, port1_pin4_config);
    
    const uint32_t port1_pin7_config = (/* Pin is configured as CTIMER2_MAT1 */
                                        IOCON_PIO_FUNC3 |
                                        /* No addition pin function */
                                        IOCON_PIO_MODE_INACT |
                                        /* Standard mode, output slew rate control is enabled */
                                        IOCON_PIO_SLEW_STANDARD |
                                        /* Input function is not inverted */
                                        IOCON_PIO_INV_DI |
                                        /* Enables digital function */
                                        IOCON_PIO_DIGITAL_EN |
                                        /* Open drain is disabled */
                                        IOCON_PIO_OPENDRAIN_DI);
    /* PORT1 PIN4 (coords: 1) is configured as CTIMER2_MAT2 */
    IOCON_PinMuxSet(IOCON, 1U, 7U, port1_pin7_config);
    
    /* Use 12 MHz clock for some of the Ctimers */
    CLOCK_AttachClk(kFRO_HF_to_CTIMER2);
    INTX_ENABLE();
    
    srcClock_Hz = CLOCK_GetCTimerClkFreq(2);
    
    sysst.clock.ct2 = srcClock_Hz;
    
    CTIMER_GetDefaultConfig(&config);

    CTIMER_Init(CTIMER, &config);

    period = srcClock_Hz>>12;
    
    CTIMER_SetupPwmPeriod(CTIMER, 1, period, period*3/400, false);
    CTIMER_SetupPwmPeriod(CTIMER, 2, period, period*3/400, false);
    
    CTIMER_StartTimer(CTIMER);
}



